package com.letian.record.service.impl;

import java.util.List;

import com.letian.common.annotation.DataScope;
import com.letian.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.letian.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.letian.record.domain.RecordPersonExtension;
import com.letian.record.mapper.RecordPersonMapper;
import com.letian.record.domain.RecordPerson;
import com.letian.record.service.IRecordPersonService;

/**
 * 记录人际关系Service业务层处理
 * 
 * @author bmz
 * @date 2023-01-15
 */
@Service
public class RecordPersonServiceImpl implements IRecordPersonService 
{
    @Autowired
    private RecordPersonMapper recordPersonMapper;

    /**
     * 查询记录人际关系
     * 
     * @param personId 记录人际关系主键
     * @return 记录人际关系
     */
    @Override
    public RecordPerson selectRecordPersonByPersonId(Long personId)
    {
        return recordPersonMapper.selectRecordPersonByPersonId(personId);
    }

    /**
     * 查询记录人际关系列表
     * 
     * @param recordPerson 记录人际关系
     * @return 记录人际关系
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "record_person")
    public List<RecordPerson> selectRecordPersonList(RecordPerson recordPerson)
    {
        return recordPersonMapper.selectRecordPersonList(recordPerson);
    }

    /**
     * 新增记录人际关系
     * 
     * @param recordPerson 记录人际关系
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecordPerson(RecordPerson recordPerson)
    {
        recordPerson.setCreateTime(DateUtils.getNowDate());
        int rows = recordPersonMapper.insertRecordPerson(recordPerson);
        insertRecordPersonExtension(recordPerson);
        return rows;
    }

    /**
     * 修改记录人际关系
     * 
     * @param recordPerson 记录人际关系
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecordPerson(RecordPerson recordPerson)
    {
        recordPerson.setUpdateTime(DateUtils.getNowDate());
        recordPersonMapper.deleteRecordPersonExtensionByPersonId(recordPerson.getPersonId());
        insertRecordPersonExtension(recordPerson);
        return recordPersonMapper.updateRecordPerson(recordPerson);
    }

    /**
     * 批量删除记录人际关系
     * 
     * @param personIds 需要删除的记录人际关系主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordPersonByPersonIds(Long[] personIds)
    {
        recordPersonMapper.deleteRecordPersonExtensionByPersonIds(personIds);
        return recordPersonMapper.deleteRecordPersonByPersonIds(personIds);
    }

    /**
     * 删除记录人际关系信息
     * 
     * @param personId 记录人际关系主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordPersonByPersonId(Long personId)
    {
        recordPersonMapper.deleteRecordPersonExtensionByPersonId(personId);
        return recordPersonMapper.deleteRecordPersonByPersonId(personId);
    }

    /**
     * 新增人物关系扩展信息信息
     * 
     * @param recordPerson 记录人际关系对象
     */
    public void insertRecordPersonExtension(RecordPerson recordPerson)
    {
        List<RecordPersonExtension> recordPersonExtensionList = recordPerson.getRecordPersonExtensionList();
        Long personId = recordPerson.getPersonId();
        if (StringUtils.isNotNull(recordPersonExtensionList))
        {
            List<RecordPersonExtension> list = new ArrayList<RecordPersonExtension>();
            for (RecordPersonExtension recordPersonExtension : recordPersonExtensionList)
            {
                recordPersonExtension.setPersonId(personId);
                list.add(recordPersonExtension);
            }
            if (list.size() > 0)
            {
                recordPersonMapper.batchRecordPersonExtension(list);
            }
        }
    }
}
